package com.gmail.dengtao.joe.game.server.config;

import org.dom4j.Document;
import org.dom4j.Node;

import com.gmail.dengtao.joe.commons.util.NumberUtils;
import com.gmail.dengtao.joe.commons.util.StringUtils;
import com.gmail.dengtao.joe.game.server.exception.AcceptorConfigInvalidException;

public class AcceptorConfig {

	private String host;
	private int port;
	private int receiveBufSize = 512;
	private int sendBufSize = 512;
	
	public AcceptorConfig(Document doc) {
		Node acceptor = doc.selectSingleNode("//server/acceptor");
		if (acceptor == null) throw new AcceptorConfigInvalidException();
		String tmp = acceptor.valueOf("@host");
		if (StringUtils.isBlank(tmp)) throw new AcceptorConfigInvalidException("invalid host!");
		else this.host = tmp;

		tmp = acceptor.valueOf("@port");
		if (NumberUtils.isInt(tmp) && NumberUtils.isPositive(tmp)) {
			this.port = Integer.parseInt(tmp);
		} else {
			throw new AcceptorConfigInvalidException("invalid host!");
		}
		
		tmp = acceptor.valueOf("@receive_buf_size");
		if (NumberUtils.isInt(tmp) && NumberUtils.isPositive(tmp)) {
			this.receiveBufSize = Integer.parseInt(tmp);
		}
		
		tmp = acceptor.valueOf("@send_buf_size");
		if (NumberUtils.isInt(tmp) && NumberUtils.isPositive(tmp)) {
			this.sendBufSize = Integer.parseInt(tmp);
		}
	}
	
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
	public int getPort() {
		return port;
	}
	public void setPort(int port) {
		this.port = port;
	}
	public int getReceiveBufSize() {
		return receiveBufSize;
	}
	public void setReceiveBufSize(int receiveBufSize) {
		this.receiveBufSize = receiveBufSize;
	}
	public int getSendBufSize() {
		return sendBufSize;
	}
	public void setSendBufSize(int sendBufSize) {
		this.sendBufSize = sendBufSize;
	}

	@Override
	public String toString() {
		return "AcceptorConfig [host=" + host + ", port=" + port
				+ ", receiveBufSize=" + receiveBufSize + ", sendBufSize="
				+ sendBufSize + "]";
	}
	
}